package ${generatorConfig.controllerGeneratorConfig.targetPackage};

import org.apache.commons.lang3.StringUtils;
import org.sky.exception.ServiceException;
import ${generatorConfig.entityGeneratorConfig.targetPackage}.${table.entityName};
import ${generatorConfig.serviceGeneratorConfig.targetPackage}.${table.entityName}Service;
import org.sky.sys.util.CriteriaUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* @author ${generatorConfig.author}
* @Title: ${table.entityName}Controller
* @ProjectName ${generatorConfig.controllerGeneratorConfig.targetProject?substring(0,generatorConfig.controllerGeneratorConfig.targetProject?index_of("/"))}
* @Description: TODO{${table.tableComment}服务类}
* @date ${.now}
* @Version 1.0
*/
@Controller
@RequestMapping(value="/${generatorConfig.requestUrl}/${table.entityName}")
public class ${table.entityName}Controller {
    @Autowired
    private ${table.entityName}Service ${table.entityName?uncap_first}Service;
    /**
    * 打开${table.tableComment}页面
    * @return
    */
    @GetMapping(value="/init${table.entityName}ListPage")
    public String init${table.entityName}ListPage(){
        return "${generatorConfig.requestUrl}/${table.tableName?lower_case?substring(table.tableName?index_of("_")+1)}/${table.entityName?lower_case}list";
    }
    /**
    * ${table.tableComment}分页查询
    * @param request
    * @return
    */
    @PostMapping(value = "/select${table.entityName}ByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody String select${table.entityName}ByPage(HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(${table.entityName}.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = ${table.entityName?uncap_first}Service.select${table.entityName}ByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageData);
    }
    /**
    * 打开${table.tableComment}新增页面
    * @return
    */
    @GetMapping(value="/init${table.entityName}AddPage")
    public String init${table.entityName}AddPage(){
        return "${generatorConfig.requestUrl}/${table.tableName?lower_case?substring(table.tableName?index_of("_")+1)}/${table.entityName?lower_case}edit";
    }
    /**
    * 打开${table.tableComment}编辑页面
    * @return
    */
    @GetMapping(value="/init${table.entityName}EditPage")
    public String init${table.entityName}EditPage(){
        return "${generatorConfig.requestUrl}/${table.tableName?lower_case?substring(table.tableName?index_of("_")+1)}/${table.entityName?lower_case}edit";
    }
    /**
    * 打开${table.tableComment}明细页面
    * @return
    */
    @GetMapping(value="/init${table.entityName}DetailPage")
    public String init${table.entityName}DetailPage(){
        return "${generatorConfig.requestUrl}/${table.tableName?lower_case?substring(table.tableName?index_of("_")+1)}/${table.entityName?lower_case}detail";
    }
    /**
    * 保存{添加/修改}${table.tableComment}
    * @param request
    * @return
    */
    @PostMapping(value = "/saveAddEdit${table.entityName}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEdit${table.entityName}(HttpServletRequest request){
        Result result = null;
        try {
            ${table.entityName} ${table.entityName?uncap_first} = RequestUtils.getEntityBean(request, ${table.entityName}.class);
            ${table.entityName?uncap_first}Service.saveAddEdit${table.entityName}(${table.entityName?uncap_first});
            result = Result.success("保存成功");
        }catch (ServiceException e){
            result = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
    /**
    * 根据Id查询${table.tableComment}
    * @param id
    * @return
    */
    @GetMapping(value = "/get${table.entityName}ById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String get${table.entityName}ById(
        @PathVariable Long id){
        ${table.entityName} bean = ${table.entityName?uncap_first}Service.get${table.entityName}ById(id);
        return JsonUtils.obj2json(bean);
    }
    /**
    * 根据ID删除${table.tableComment}
    * @param request
    * @return
    */
    @PostMapping(value = "/del${table.entityName}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String del${table.entityName}(HttpServletRequest request){
        Result result= null;
        try {
            String delRows=request.getParameter("delRows");
            List<${table.entityName}> de=JsonUtils.json2list(delRows, ${table.entityName}.class);
            ${table.entityName?uncap_first}Service.del${table.entityName}ById(de);
            result = Result.success("删除成功");
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
}